'''
LastEditors: 杜康
LastEditTime: 2021-11-29 15:50:21
'''
from urllib.request import urlopen
# 处理404跟500
from urllib.error import HTTPError
# 处理URL错误
from urllib.error import URLError
from bs4 import BeautifulSoup
try:
    html = urlopen('http://pythonscraping.com/pages/page1.html')
except HTTPError as e:
    print(e)
except URLError as e:
    print('无法连接到服务器！')
else:
    print('执行有效！')
bs = BeautifulSoup(html.read(), 'html.parser')  # 将获取到的页面转成一个BS对象
# 通过对BS对象进行检查
try:
    badContent = bs.nonExistingTag.anotherTag
except AttributeError as e:
    print('属性问题 - 未找到对应标签')
else:
    if badContent == None:
        print('标签问题 - 未找到对应标签')
    else:
        print(badContent)
print(bs.h1)